<?php
    include_once("../Dao/db.php");
    Class AdminSrch
    {
        var $errorMessage;

        var $memberInfoList="";

        var $memberInfoCount;

        var $fromToPage;

        var $nowPageNumber;

        var $pageNums;

        var $cmbStatus;

        var $user_name;

        // 毎ページのレコード数
        var $RECORD_NUMBER_PER_PAGE = 20;

        function mytask()
        {

            $this->checkMemeber();

            if($_POST["act"] == "submit")
            {
                if($_POST["cc_change"] == "1")
                {
                //アカウント登録フラグ更新を行う
                $cc_status=$_POST["cc_status"];
                $cc_memberId=$_POST["cc_memberId"];
                $query = "UPDATE member SET ";
                $query .= "ccStatus=$cc_status WHERE ";
                $query .= "memberId=$cc_memberId";
                $result = mysql_query($query);
                if(!$result)
                {
                    echo $query;
                }
                }
                $this->errorMessage = "";
                $this->user_name  = $_POST["user_name"];
                $this->nowPageNumber = 1;
                if ($_POST["user_name"] !=null && $_POST["user_name"] != "")
                {

                    $this->user_name = $_POST["user_name"] ;
                }
                else
                {
                    $this->user_name = "";
                }
                if ( $_POST["cmbStatus"] == "on")
                {
                    $this->cmbStatus  = "checked";
                }
                else
                {
                    $this->cmbStatus = "";
                }
            }
            else if ($_POST["act"] == "delete")
            {
                $this->errorMessage = "";
                // 選択したデータを削除
                $this->deleteMemberInfo($_POST["memberId"]);

                if ($_GET["nowPageNumber"] !=null && $_GET["nowPageNumber"] != "")
                {
                    $this->nowPageNumber = $_GET["nowPageNumber"] ;
                }
                else
                {
                    $this->nowPageNumber = 1;
                }
                $this->user_name  = $_POST["user_name"];

                if ($_POST["user_name"] !=null && $_POST["user_name"] != "")
                {

                    $this->user_name = $_POST["user_name"] ;
                }
                else
                {
                    $this->user_name = "";
                }
                if ( $_POST["cmbStatus"] == "on")
                {
                    $this->cmbStatus  = "checked";
                }
                else
                {
                    $this->cmbStatus = "";
                }
            }
            else if ($_POST["act"] == "leave")
            {
                $this->errorMessage = "";
                // 選択したデータを削除
                $this->leaveMemberInfo($_POST["memberId"]);

                if ($_GET["nowPageNumber"] !=null && $_GET["nowPageNumber"] != "")
                {
                    $this->nowPageNumber = $_GET["nowPageNumber"] ;
                }
                else
                {
                    $this->nowPageNumber = 1;
                }
                $this->user_name  = $_POST["user_name"];

                if ($_POST["user_name"] !=null && $_POST["user_name"] != "")
                {

                    $this->user_name = $_POST["user_name"] ;
                }
                else
                {
                    $this->user_name = "";
                }
                if ( $_POST["cmbStatus"] == "on")
                {
                    $this->cmbStatus  = "checked";
                }
                else
                {
                    $this->cmbStatus = "";
                }
            }
            else
            {
                $this->errorMessage = "";
                if ($_GET["nowPageNumber"] !=null && $_GET["nowPageNumber"] != "")
                {
                    $this->nowPageNumber = $_GET["nowPageNumber"] ;
                } else
                {
                    $this->nowPageNumber = 1;
                }
                if ($_GET["userName"] !=null && $_GET["userName"] != "")
                {
                    $this->user_name = $_GET["userName"] ;
                } else
                {
                    $this->user_name = "";
                }
                if ($_GET["cmbStatus"] !=null && $_GET["cmbStatus"] != "")
                {
                    $this->cmbStatus = $_GET["cmbStatus"] ;
                }
                else
                {
                    $this->cmbStatus = "";
                }
            }
            // 検索処理を行う
            $this->doSearch();
        }


        function doSearch()
        {
            $this->initialPageData();
            $this->getMemberInfoList();
        }

        function initialPageData()
        {
            $this->memberInfoList = "";
            $this->fromToPage = "";
            $this->pageNums = "";
        }

        function getMemberInfoList()
        {

            $fromPageNumber = ($this->nowPageNumber-1) * $this->RECORD_NUMBER_PER_PAGE;
            $this->getMemberInfoCount ();
            $sql = "select memberId, loginid, password,company, ";
            $sql .= "post, name, telephone, email, scale, ";
            $sql .= "industryId, address, section, fax, postalCode,";
            $sql .= "`status`, sync,ccStatus,ccStatus,memberId,";
            $sql .= "lastloginDate, registDate from member ";

            //退会済みも表示をチェックしたら、条件に追加
            if($this->cmbStatus != "checked")
            {
                $where = " where `status` <> 3 ";
            }

            //ユーザ名入力した場合、条件に追加
            if ($this->user_name !=null && $this->user_name != "")
            {
                if($where)
                {
                    $where .= " and loginid like '%".$this->user_name."%' ";
                    
                }
                else
                {
                    $where = " where loginid like '%".$this->user_name."%' ";
                    
                }
            }

            //登録状況指定する場合、条件追加
            if($_POST['is_incenter'] == 1)
            {
                if($where) $where .= " and ccStatus=1 ";
                else $where = " where ccStatus=1 ";
            }
            else if($_POST['is_incenter'] == 2)
            {
                if($where) $where .= " and ccStatus=2 ";
                else $where = " where ccStatus=2 ";
            }
            else if($_POST['is_incenter'] == 3)
            {
                if($where) $where .= " and ccStatus=3 ";
                else $where = " where ccStatus=3 ";
            }

            //同期状況指定する場合、条件追加
            if($_POST['sync_status'] == 1)
            {
                if($where) $where .= " and sync=1 ";
                else $where = " where sync=1 ";
            }
            else if($_POST['sync_status'] == 2)
            {
                if($where) $where .= " and sync=2 ";
                else $where = " where sync=2 ";
            }
            else if($_POST['sync_status'] == 3)
            {
                if($where) $where .= " and sync=3 ";
                else $where = " where sync=3 ";
            }

            $sql .= $where;
            $sql = $sql." order by memberId";
            $toPageNumber = $fromPageNumber  + $this->RECORD_NUMBER_PER_PAGE;
            $sql = $sql." limit ".$fromPageNumber.",".$this->RECORD_NUMBER_PER_PAGE;
            $result = mysql_query($sql);
            $rows = mysql_num_rows($result);
            if ($rows > 0)
            {
                $fromPageNumber = $fromPageNumber + 1;
                $toPageNumber  = $fromPageNumber + $rows -1;
                $this->fromToPage = $fromPageNumber."件-".$toPageNumber."件";
                for ($i=0;$i<$rows;$i++)
                {
					if($i%2==0)
					{
						$this->memberInfoList  = $this->memberInfoList. " <tr class='tr3' > " ;
					}else{
						$this->memberInfoList  = $this->memberInfoList. " <tr class='tr4' > " ;
					}
                    $data = mysql_fetch_row($result);
                    for ($j=0;$j<21;$j++)
                    {			
                        $colValue = $data[$j];
                        if ($j==0)
                        {
                            $this->memberInfoList  = $this->memberInfoList. "<td align='center'>".$colValue."</td>" ;
                            continue;
                        }

                        if ($j==14)
                        {
                            switch($colValue)
                            {
                                case 1: $colValue="アクティブ"; break;
                                case 2: $colValue="一時停止"; break;
                                case 3: $colValue="退会済み"; break;
                                default :$colValue="";break;
                            }
                        }
                        //同期状況
                        else if($j == 15)
                        {
                            if($colValue == 1)
                            {
                                $colValue = "同期済み";
                            }
                            else if($colValue == 2)
                            {
                                $colValue = "未同期";
                            }
                            else if($colValue == 3)
                            {
                                $colValue = "エラー";
                            }
                        }
                        //同期バッチボタン
                        else if($j == 16)
                        {
                            if($colValue == 1)
                            {
                                $colValue = "<input type='button' value='バッチ起動' class='navText' onclick=";
                                $colValue .= "window.open('SynchronizeLog.php?memberId={$data[0]}') />";
                            }
                            else
                            {
                                $colValue = "<input disabled type='button' value='バッチ起動' class='navText' />";
                            }
                        }
                        //登録状況フラグ
                        else if($j == 17)
                        {
                            if($colValue == 1)
                            {
                                $colValue = "<select class='txBox' onchange=ccStatusChange({$data[0]},this.value)>";
                                $colValue .= "<option value='1' selected>登録済み</option>";
                                $colValue .= "<option value='2'>未登録</option>";
                                $colValue .= "<option value='3'>その他</option>";
                                $colValue .= "</select>";
                            }
                            else if($colValue == 2)
                            {
                                $colValue = "<select class='txBox' onchange=ccStatusChange({$data[0]},this.value)>";
                                $colValue .= "<option value='1'>登録済み</option>";
                                $colValue .= "<option value='2' selected>未登録</option>";
                                $colValue .= "<option value='3'>その他</option>";
                                $colValue .= "</select>";
                            }
                            else if($colValue == 3)
                            {
                                $colValue = "<select class='txBox' onchange=ccStatusChange({$data[0]},this.value)>";
                                $colValue .= "<option value='1'>登録済み</option>";
                                $colValue .= "<option value='2'>未登録</option>";
                                $colValue .= "<option value='3' selected>その他</option>";
                                $colValue .= "</select>";
                            }
                        }
                        //アカウント情報ポップアップボタン
                        else if($j == 18)
                        {
                            $query = "SELECT account_name,password FROM account WHERE ";
                            $query .= "memberId=$colValue";
                            $results = mysql_query($query);
                            $row = mysql_fetch_array($results);
                            $colValue = "<input type='button' class='navText' value='情報' onclick=showInfo('{$row['account_name']}','{$row['password']}') />";

                        }
                        else if ($j==19 || $j==20)
                        {
                            $colValue = date("Y/m/d", strtotime($colValue));
                        }
                        else if($j==9)
                        {
                            $industrySql = "select name from industry where industryId = ".$colValue;
                            $industryResult = mysql_query($industrySql);
                            $industryRows = mysql_num_rows($industryResult);
                            if ($industryRows > 0)
                            {
                                $industryData = mysql_fetch_row($industryResult);
                                $colValue  = $industryData[0];                                ;
                            }
                            else
                            {
                                $colValue = "";
                            }
                        }
                        $this->memberInfoList =  $this->memberInfoList."<td align='left'>".$colValue."</td>";

                    }

                    $this->memberInfoList  = $this->memberInfoList."<td><input type='button' value='退会' class='navText' onclick='javascript:leaveMember(".$data[0].")'></td>";
                    $this->memberInfoList  = $this->memberInfoList."<td><input type='button' class ='btn del' onclick='javascript:delMember(".$data[0].")'></td>";
                    $this->memberInfoList  = $this->memberInfoList."</tr>";
                }
            }
            else if ($this->nowPageNumber > 1)
            {
                $this->nowPageNumber = $this->nowPageNumber - 1;
                $this->doSearch();
            }
            else
            {
                $this->errorMessage  =  "データが存在していません、ご確認ください。";
            }
        }

        function  getMemberInfoCount()
        {
            $sql = "select count(memberId) from member";
            //退会済みも表示をチェックしたら、条件に追加
            if($this->cmbStatus != "checked")
            {
                $where = " where `status` <> 3 ";
            }

            //ユーザ名入力した場合、条件に追加
            if ($this->user_name !=null && $this->user_name != "")
            {
                if($where)
                {
                    $where .= " and loginid like '%".$this->user_name."%' ";

                }
                else
                {
                    $where = " where loginid like '%".$this->user_name."%' ";

                }
            }

            //登録状況指定する場合、条件追加
            if($_POST['is_incenter'] == 1)
            {
                if($where) $where .= " and ccStatus=1 ";
                else $where = " where ccStatus=1 ";
            }
            else if($_POST['is_incenter'] == 2)
            {
                if($where) $where .= " and ccStatus=2 ";
                else $where = " where ccStatus=2 ";
            }
            else if($_POST['is_incenter'] == 3)
            {
                if($where) $where .= " and ccStatus=3 ";
                else $where = " where ccStatus=3 ";
            }

            //同期状況指定する場合、条件追加
            if($_POST['sync_status'] == 1)
            {
                if($where) $where .= " and sync=1 ";
                else $where = " where sync=1 ";
            }
            else if($_POST['sync_status'] == 2)
            {
                if($where) $where .= " and sync=2 ";
                else $where = " where sync=2 ";
            }
            else if($_POST['sync_status'] == 3)
            {
                if($where) $where .= " and sync=3 ";
                else $where = " where sync=3 ";
            }
            $sql .= $where;

            $result = mysql_query($sql);
            $cnt = mysql_fetch_row($result);
            $this->memberInfoCount =  "/".$cnt[0]."件";
            if ($cnt[0] >0 )
            {
                $this->nowPage  = $this->nowPage."<td>ページ </td>";
            }
            for ($i=0;$i < $cnt[0]/$this->RECORD_NUMBER_PER_PAGE;$i++)
            {
                $page = $i + 1;
                if ($this->nowPageNumber == $page)
                {
                    $this->pageNums = $this->pageNums."<td width='10'>
                                                <a href='AdminSrch.php?nowPageNumber=".$page;
                    if ($this->user_name !=null && $this->user_name != "")
                    {
                        $this->pageNums = $this->pageNums."&userName=".$this->user_name ;
                    }
                    if ( $this->cmbStatus == "checked")
                    {
                        $this->pageNums = $this->pageNums."&cmbStatus=checked" ;
                    }
                    $this->pageNums = $this->pageNums."'>"."[".$page."]";
                }
                else
                {
                    $this->pageNums = $this->pageNums."<td width='10'>
                                                <a href='AdminSrch.php?nowPageNumber=".$page;
                    if ($this->user_name !=null && $this->user_name != "")
                    {
                        $this->pageNums = $this->pageNums."&userName=".$this->user_name ;
                    }
                    if ( $this->cmbStatus == "checked")
                    {
                        $this->pageNums = $this->pageNums."&cmbStatus=checked" ;
                    }
                    $this->pageNums = $this->pageNums."'>".$page;
                }
                $this->pageNums = $this->pageNums."</a></td>";
            }
        }

        function deleteMemberInfo($memberId)
        {

            $deleteCount = 0;
            $sql = "update account set status=0 where memberId='".$memberId."'";
            mysql_query($sql);						
			
            $sql = "delete from member where memberId='".$memberId."'";
            $deleteCount = mysql_query($sql);
            if ($deleteCount == 0)
            {
                $this->errorMessage = "ＤＢエラーが発生しました。";
                return;
            }
        }

        function leaveMemberInfo($memberId)
        {

            $deleteCount = 0;
            $sql = "update account set status=0 where memberId='".$memberId."'";
            mysql_query($sql);			
			
            $sql = "update member set status=3 where memberId='".$memberId."'";
            $upCount = mysql_query($sql);
            if ($upCount == 0)
            {
                $this->errorMessage = "ＤＢエラーが発生しました。";
                return;
            }
        }

        function checkMemeber()
        {
            $sql = "select memberId,company,management from member where memberId=".$_SESSION["memberId"];

            $result = mysql_query($sql);
            $number_of_rows = mysql_num_rows($result);
            if ($number_of_rows < 1)
            {//echo $sql." ".$number_of_rows;
                header("Location:../Login.php");
            }
            else
            {
                $data = mysql_fetch_row($result);
                if($data[2] <> 1) //Alitoの場合
                {
                    header("Location:../Login.php");
                }
            }
        }
    }
?>

